﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
public class Primitives2D
{
    public static Texture2D dot;
    public static void DrawLine(SpriteBatch sb, Vector2 p1, Vector2 p2, float Width, Color whichColor)
    {
        Vector2 mid = (p1 + p2) / 2;
        double rotation = AngleBetweenVectors(p1, p2);
        float distance = DistanceBetweenVectors(p1, p2) / 2;
        Vector2 scale = new Vector2(distance, dot.Height * Width);
        Vector2 origin = new Vector2(1, 1);
        sb.Draw(dot, mid, null, whichColor, (float)rotation, origin, scale, SpriteEffects.None, 0);
    }

    public static void DrawCircle(SpriteBatch sb, Vector2 p, float radius, float Width, Color whichColor, int dotCount)
    {
        float interval = (float)(2 * Math.PI) / dotCount;
        List<Vector2> points = new List<Vector2>();

        for (float f = 0; f < 2 * Math.PI; f += interval)
        {
            points.Add(new Vector2(p.X + radius * (float)Math.Sin(f), p.Y + radius * (float)Math.Cos(f)));
        }

        DrawXSidedFigure(sb, points, Width, whichColor);
    }

    public static void DrawRect(SpriteBatch sb, Vector2 min, Vector2 max, float Width, Color whichColor)
    {
        Vector2 p1 = min;
        Vector2 p2 = new Vector2(min.X, max.Y);
        Vector2 p3 = max;
        Vector2 p4 = new Vector2(max.X, min.Y);
        DrawLine(sb, p1, p2, Width, whichColor);
        DrawLine(sb, p2, p3, Width, whichColor);
        DrawLine(sb, p3, p4, Width, whichColor);
        DrawLine(sb, p4, p1, Width, whichColor);
    }

    public static void DrawBoundingBox(SpriteBatch sb, BoundingBox b)
    {
        Vector2 min = new Vector2(b.Min.X, b.Min.Y);
        Vector2 max = new Vector2(b.Max.X, b.Max.Y);
        DrawRect(sb, min, max, 0.5f, Color.Red);
    }

    public static void DrawTriangle(SpriteBatch sb, Vector2 p1, Vector2 p2, Vector2 p3, float Width, Color whichColor)
    {
        DrawLine(sb, p1, p2, Width, whichColor);
        DrawLine(sb, p2, p3, Width, whichColor);
        DrawLine(sb, p3, p1, Width, whichColor);
    }

    public static void DrawXSidedFigure(SpriteBatch sb, List<Vector2> points, float Width, Color whichColor)
    {
        foreach (Vector2 p in points)
        {
            if (points.IndexOf(p) + 1 < points.Count)
                DrawLine(sb, p, points[points.IndexOf(p) + 1], Width, whichColor);
        }
        DrawLine(sb, points[points.Count - 1], points[0], Width, whichColor);
    }

    public static void FillRect(SpriteBatch sb, Vector2 min, Vector2 max, Color whichColor)
    {
        sb.Draw(dot, min, null, whichColor, 0f, Vector2.Zero, (max - min) * 1 / new Vector2(dot.Width, dot.Height), SpriteEffects.None, 0f);
    }

    public static float DistanceBetweenVectors(Vector2 v1, Vector2 v2)
    {
        return (float)Math.Sqrt((v2.X - v1.X) * (v2.X - v1.X) + (v2.Y - v1.Y) * (v2.Y - v1.Y));
    }

    public static float AngleBetweenVectors(Vector2 v1, Vector2 v2)
    {
        float x1 = v1.X;
        float x2 = v2.X;
        float y1 = v1.Y;
        float y2 = v2.Y;
        return (float)Math.Atan2(y2 - y1, x2 - x1);
    }
}
